/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author admin
 */
public class Yudisium {

    private static java.sql.Connection conn;
    private String nim;
    private String nama;
    private String prodi;
    private String nomor_hp;
    private java.sql.Date periode_yudisium;
    private boolean daftar_nilai;
    private boolean naskah_ta;
    private boolean bebas_pinjam_lab;
    private boolean bebas_pinjam_dos;
    private boolean penyerahan_KP;
    private boolean keterangan_kkl;

    /**
     * @return the nim
     */
    public String getNim() {
        return nim;
    }

    /**
     * @param nim the nim to set
     */
    public void setNim(String nim) {
        this.nim = nim;
    }

    /**
     * @return the nama
     */
    public String getNama() {
        return nama;
    }

    /**
     * @param nama the nama to set
     */
    public void setNama(String nama) {
        this.nama = nama;
    }

    /**
     * @return the prodi
     */
    public String getProdi() {
        return prodi;
    }

    /**
     * @param prodi the prodi to set
     */
    public void setProdi(String prodi) {
        this.prodi = prodi;
    }

    /**
     * @return the nomor_hp
     */
    public String getNomor_hp() {
        return nomor_hp;
    }

    /**
     * @param nomor_hp the nomor_hp to set
     */
    public void setNomor_hp(String nomor_hp) {
        this.nomor_hp = nomor_hp;
    }

    /**
     * @return the periode_yudisium
     */
    public java.sql.Date getPeriode_yudisium() {
        return periode_yudisium;
    }

    /**
     * @param periode_yudisium the periode_yudisium to set
     */
    public void setPeriode_yudisium(java.sql.Date periode_yudisium) {
        this.periode_yudisium = periode_yudisium;
    }

    /**
     * @return the daftar_nilai
     */
    public boolean isDaftar_nilai() {
        return daftar_nilai;
    }

    /**
     * @param daftar_nilai the daftar_nilai to set
     */
    public void setDaftar_nilai(boolean daftar_nilai) {
        this.daftar_nilai = daftar_nilai;
    }

    /**
     * @return the naskah_ta
     */
    public boolean isNaskah_ta() {
        return naskah_ta;
    }

    /**
     * @param naskah_ta the naskah_ta to set
     */
    public void setNaskah_ta(boolean naskah_ta) {
        this.naskah_ta = naskah_ta;
    }

    /**
     * @return the bebas_pinjam_lab
     */
    public boolean isBebas_pinjam_lab() {
        return bebas_pinjam_lab;
    }

    /**
     * @param bebas_pinjam_lab the bebas_pinjam_lab to set
     */
    public void setBebas_pinjam_lab(boolean bebas_pinjam_lab) {
        this.bebas_pinjam_lab = bebas_pinjam_lab;
    }

    /**
     * @return the bebas_pinjam_dos
     */
    public boolean isBebas_pinjam_dos() {
        return bebas_pinjam_dos;
    }

    /**
     * @param bebas_pinjam_dos the bebas_pinjam_dos to set
     */
    public void setBebas_pinjam_dos(boolean bebas_pinjam_dos) {
        this.bebas_pinjam_dos = bebas_pinjam_dos;
    }

    /**
     * @return the penyerahan_KP
     */
    public boolean isPenyerahan_KP() {
        return penyerahan_KP;
    }

    /**
     * @param penyerahan_KP the penyerahan_KP to set
     */
    public void setPenyerahan_KP(boolean penyerahan_KP) {
        this.penyerahan_KP = penyerahan_KP;
    }

    /**
     * @return the keterangan_kkl
     */
    public boolean isKeterangan_kkl() {
        return keterangan_kkl;
    }

    /**
     * @param keterangan_kkl the keterangan_kkl to set
     */
    public void setKeterangan_kkl(boolean keterangan_kkl) {
        this.keterangan_kkl = keterangan_kkl;
    }

    private static java.sql.Connection getConnection() {
        try {
            DriverManager.registerDriver(new com.mysql.jdbc.Driver());
            conn = (DriverManager.getConnection("jdbc:mysql://localhost:3306/yudisium", "root", ""));
            return conn;
        } catch (SQLException ex) {
            Logger.getLogger(Yudisium.class.getName()).log(Level.SEVERE, null, ex);
            return conn;
        }
    }

    public static boolean setPendaftaranYudisium(Yudisium yud) throws SQLException {
        if (getConnection() != null) {
            String sql = "insert into yudisium (nim,nama,prodi,nomor_hp,periode_yudisium,"
                    + "daftar_nilai,naskah_ta,bebas_pinjam_lab,bebas_pinjam_dos,penyerahan_KP,"
                    + "keterangan_kkl) values (?,?,?,?,?,?,?,?,?,?,?);";
            PreparedStatement ps = getConnection().prepareStatement(sql);
            ps.setString(1, yud.getNim());
            ps.setString(2, yud.getNama());
            ps.setString(3, yud.getProdi());
            ps.setString(4, yud.getNomor_hp());
            ps.setDate(5, yud.getPeriode_yudisium());
            ps.setBoolean(6, yud.isDaftar_nilai());
            ps.setBoolean(7, yud.isNaskah_ta());
            ps.setBoolean(8, yud.isBebas_pinjam_lab());
            ps.setBoolean(9, yud.isBebas_pinjam_dos());
            ps.setBoolean(10, yud.isPenyerahan_KP());
            ps.setBoolean(11, yud.isKeterangan_kkl());
            ps.executeUpdate();
            return true;
        } else {
            JOptionPane.showMessageDialog(null, "\nTidak dapat melakukan koneksi ke database. Periksa koneksi ke database anda."
                    + "\nMisal. Koneksi jaringan atau Peraturan koneksi ke database anda.", "Kesalahan Koneksi", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }

    public static ResultSet getDaftarPeserta(java.sql.Date mulai, java.sql.Date akhir) throws SQLException {
        String mul = mulai.toString();
        String akh = akhir.toString();
        String sql = "select * from yudisium where periode_yudisium between '" + mul + "' and '" + akh + "';";
        Statement st = getConnection().createStatement();
        ResultSet results = st.executeQuery(sql);
        return results;
    }

    public static boolean editKelengkapanYudisium(Yudisium baru, Yudisium lama) throws SQLException {
        if (getConnection() != null) {
            String sql = "update yudisium set daftar_nilai=?, naskah_ta=?, bebas_pinjam_lab=?, bebas_pinjam_dos=?, penyerahan_KP=?, keterangan_kkl=? "
                    + "where nim=?;";
            PreparedStatement ps = getConnection().prepareStatement(sql);
            ps.setBoolean(1, baru.isDaftar_nilai());
            ps.setBoolean(2, baru.isNaskah_ta());
            ps.setBoolean(3, baru.isBebas_pinjam_lab());
            ps.setBoolean(4, baru.isBebas_pinjam_dos());
            ps.setBoolean(5, baru.isPenyerahan_KP());
            ps.setBoolean(6, baru.isKeterangan_kkl());
            ps.setString(7, lama.getNim());
            ps.executeUpdate();
            return true;
        } else {
            return false;
        }
    }
}
